package com.itheima.web.controller;

import com.itheima.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

/**
 * @author huyoufu <https://github.com/huyoufu>
 * @TIME 2020-08-11 14:19
 * @description
 *
 * 总结一下 到底都支持什么样子的参数
 *
 *
 */
@Controller
@RequestMapping("/params")
public class ParameterController {

    /*-----------------演示支持servlet 体系 request response session-----------------------------------------------------*/
    @RequestMapping("/foo1")
    public String foo1(HttpServletRequest request, HttpServletResponse response, HttpSession session){

        System.out.println("演示servlet提示的");
        return "param/success";
    }
    /*-----------------演示支持servlet 体系-----------------------------------------------------*/




    /*------------------演示支持springmvc体系 modelandview 和model对象 ------------------------*/

    @RequestMapping("/foo2")
    public ModelAndView foo2(ModelAndView modelAndView){
        modelAndView.setViewName("param/success");

        return modelAndView;
    }
    @RequestMapping("/foo3")
    public String foo3(Model model){
        //model对象 负责帮助往request域中放数据用
        model.addAttribute("info","xxx");

        return "param/success";
    }
    /*------------------演示支持springmvc体系 modelandview 和model对象 ------------------------*/





    /*------------------演示支持 普通前端传递的参数 string Integer Double .... ------------------*/

    @RequestMapping("/foo4")
    public String foo4(
            @RequestParam(value = "name",required = false) String name,
            @RequestParam(value = "age")Integer age,
            @RequestParam(value = "yue")Double yue){

        System.out.println("获取普通前端给的参数:"+"name-"+name+"----age:"+age+"---余额:"+yue);
        return "param/success";
    }
    /*------------------演示支持 普通前端传递的参数 string Integer Double------------------------*/



    /*------------------演示支持对象类型参数----------------------------------------------------*/

    @RequestMapping("/foo5")
    public String foo5(User user){
        System.out.println(user);

        return "param/success";
    }
    /*------------------演示支持对象类型参数----------------------------------------------------*/


    /*------------------前端能不能传数组呢?----------------------------------------------------*/
    @RequestMapping("/foo6")
    public String foo6(
            @RequestParam(value = "username") String name,
            String[] hobbies){

        System.out.println("获取:"+name+"的爱好:"+ Arrays.toString(hobbies));


        return "param/success";
    }

    /*------------------前端能不能传数组呢?----------------------------------------------------*/

    /*------------------演示获取cookie和请求头的信息-------------------------------------------*/

    @RequestMapping("/foo7")
    public String foo7(@RequestHeader("user-agent")String userAgent){
//        String userAgent = request.getHeader("user-agent");
        System.out.println("获取请求头:"+userAgent);

        return "param/success";
    }

    @RequestMapping("/foo8")
    public String foo8(@CookieValue("JSESSIONID")String cookie) {
//        Cookie[] cookies = request.getCookies();
//        System.out.println("获取cookie:"+Arrays.toString(cookies));
        System.out.println("cookie值:"+cookie);
        return "param/success";
    }
    /*------------------演示获取cookie和请求头的信息-------------------------------------------*/


    /*------------------演示日期类型的参数--------------------------------------------------*/

    @RequestMapping("/foo9")
    public String foo9(User user) {
        System.out.println("获取用户信息:"+user);

        return "param/success";
    }

}
